package com.changgou.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

/**
 * @version v1.0
 * @ProjectName: changgou_parent
 * @ClassName: IPFilter
 * @Description: TODO(获取客户端请求的ip)
 * @Author: Xu Fj
 * @Date: 2020/2/24 11:22
 */
@Component
public class IPFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("过滤器1执行了啊啊啊 啊啊啊啊 啊啊");
        //获取request请求对象
        ServerHttpRequest request = exchange.getRequest();
        //通过request对象获取路由地址
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        System.out.println(remoteAddress.getHostName());
        return chain.filter(exchange);
    }

    //这是规定过滤器的优先级,值越小优先级越高就会先执行
    @Override
    public int getOrder() {
        return 1;
    }
}
